package com.ly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.entity.News;
import com.ly.mapper.NewsMapper;
import com.ly.pojo.dto.NewsDTO;
import com.ly.pojo.vo.NewsVO;
import com.ly.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : 黑渊白花
 * @ClassName NewsServiceImpl
 * @date : 2025/2/8 22:31
 * @Description
 */
@Service
@Slf4j
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public IPage<NewsVO> getAllPage(NewsDTO newsDTO) {
        Integer pageNum = newsDTO.getPageNum();
        Integer pageSize = newsDTO.getPageSize();
        String title = newsDTO.getTitle();
        String teamName = newsDTO.getTeamName();
        IPage<NewsVO> page = new Page<>(pageNum,pageSize);
        IPage<NewsVO> allPage = newsMapper.getAllPage(page,title, teamName);
        return allPage;
    }

    @Override
    public IPage<NewsVO> getPage(NewsDTO newsDTO) {
        Integer pageNum = newsDTO.getPageNum();
        Integer pageSize = newsDTO.getPageSize();
        String title = newsDTO.getTitle();
        Integer teamId = newsDTO.getTeamId();
        IPage<NewsVO> page = new Page<>(pageNum,pageSize);
        IPage<NewsVO> stPage = newsMapper.getPage(page,teamId,title);
        return stPage;
    }

    @Override
    public List<NewsVO> getNewsClickNum() {
        // QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        // queryWrapper.orderByDesc("click_num").last("limit 10");
        // List<News> news = newsMapper.selectList(queryWrapper);
        List<NewsVO> newsClickNum = newsMapper.getNewsClickNum();
        return newsClickNum;
    }

    @Override
    public Long getNewsNumByTeamId(Integer teamId) {
        QueryWrapper<News> qw = new QueryWrapper<News>();
        qw.eq("team_id", teamId);
        Long count = newsMapper.selectCount(qw);
        return count;
    }

    @Override
    public List<News> getNewsLast(Integer teamId) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("team_id",teamId).orderByDesc("create_time").last("limit 5");
        List<News> news = newsMapper.selectList(queryWrapper);
        return news;
    }

    @Override
    public void addClickNum(News news) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",news.getId());
        wrapper.setSql("click_num = click_num + 1");
        newsMapper.update(wrapper);
    }
}
